package thread;

public class DeadLock implements Runnable {
    private String mirror="mirror";
    private String lipstick="lipstick";
    private boolean flag;

    public DeadLock(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if (flag) {
            synchronized (mirror) {
                System.out.println(Thread.currentThread().getName()+"获得镜子的锁！");
                synchronized (lipstick) {
                    System.out.println(Thread.currentThread().getName()+"获得口红的锁！");
                }
            }
        } else {
            synchronized (lipstick) {
                System.out.println(Thread.currentThread().getName()+"获得口红的锁！");
                synchronized (mirror) {
                    System.out.println(Thread.currentThread().getName()+"获得镜子的锁！");
                }
            }
        }
    }
}

class TestDeadLock {
    public static void main(String[] args) {
        new Thread(new DeadLock(true)).start();
        new Thread(new DeadLock(false)).start();
    }
}
